﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : MonoBehaviour
{
    private float speed =5.0f;
    private bool isjump = true;
    private Vector3 camerarotation;
    private Transform cameraTransform;     // 摄像机的Transform组件  
    void Start()
    {
        cameraTransform = transform.Find("Main Camera").GetComponent<Transform>();//获取相机transform组件,camera放到cube的子物体下面
    }

    void Update()
    {
        //让物体旋转角度和相机一致
        camerarotation = cameraTransform.eulerAngles;
        transform.eulerAngles = new Vector3(0, camerarotation.y, 0);
        //隐藏鼠标       
        Cursor.lockState = CursorLockMode.Locked;                    
        move();
                                              

    }
    void jump()
    {
        GetComponent<Rigidbody>().AddForce(0, 300, 0);
        isjump = false;
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == "ground")
        {
            isjump = true;
        }
    }
    private void move()
    {
        float m;
        float n;
        if (Input.GetKey(KeyCode.W))                                          //移动
        {
            transform.Translate(Vector3.forward * Time.deltaTime * speed);
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(Vector3.forward * Time.deltaTime * -speed);
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(Vector3.right * Time.deltaTime * -speed);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * Time.deltaTime * speed);
        }

        if (Input.GetKeyDown(KeyCode.Space) && isjump)
        {
            jump();
        }
        if(Input.GetKey(KeyCode.LeftControl))
        {
            cameraTransform.position =(new Vector3(0, -0.5f, 0));
        }
        if(Input.GetKeyUp(KeyCode.LeftControl))
        {
            cameraTransform.position = (new Vector3(0, 1, 0));
        }
    }
}

